package com.googlecode.gwtbb.client.only.widget.form;

import com.googlecode.gwtbb.client.only.remote.ClientSession;
import com.googlecode.gwtbb.client.only.widget.form.user.LoginForm;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Widget;

/**
 * @author Abraham Grief
 * @version $Id$
 * @since June 10, 2009
 */
public abstract class LoginProtectedClickListener implements ClickListener {
	private FormDialog loginDialog;
	private ClientSession session;

	public LoginProtectedClickListener(ClientSession session) {
		this.session = session;
	}

	public abstract void ifLoggedIn();

	@Deprecated
	public void onClick(Widget sender) {
		if (session.isLoggedOut()) {
			if (loginDialog == null) {
				loginDialog = new FormDialog(new LoginForm(session));
				loginDialog.getFormWidget().getCompleteProcessListenerCollection().addCompleteProcessListener(new CompleteProcessListener() {
					public void onProcessCompletion(ProcessEvent processEvent) {
						ifLoggedIn();
					}
				});
			}
			loginDialog.getFormWidget().reset();
			loginDialog.center();
		} else {
			ifLoggedIn();
		}
	}
}
